package com.osi.controller;

import com.osi.bean.Admin;
import com.osi.bean.Logininfo;
import com.osi.bean.Student;
import com.osi.service.AdminService;
import com.osi.service.StudentService;
import com.osi.utils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author lixin
 * @date 2021/2/8 17:24
 */
@Controller
public class LoginController {


    @Autowired
    private StudentService studentService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;


    /**
     * 项目启动跳转到登录页
     *
     * @param model
     * @return
     */
    @GetMapping({"/", "/login"})
    public String initLogin(ModelMap model) {
        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;

        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("username")) {
                username = cookie.getValue();
            }
            if (cookie.getName().equals("password")) {
                password = cookie.getValue();
            }
        }
        model.addAttribute("username", username);
        model.addAttribute("password", password);

        return "login";
    }

    /**
     * 用户登录操作
     *
     * @param params
     * @param model
     * @return
     */
    @PostMapping("/login")
    public String login(@RequestParam Map<String, Object> params, Model model) {


        String username = params.get("username").toString();
        String password = params.get("password").toString();



        String type = params.get("type").toString();
        HttpSession session = request.getSession();
        if ("student".equals(type)) {
            Student student = studentService.login(username, password);
            if (student == null) {
                model.addAttribute("msg", "学生信息不存在");
                return "/login";
            } else {
                UserContext.putCurrebtUser(new Logininfo(student.getUsername(), student.getPassword()));
                model.addAttribute("student", student);

                if (params.get("remember") != null) {
                    // 账号信息保存三天
                    Cookie c1 = new Cookie("username", username);
                    Cookie c2 = new Cookie("password", password);
                    c1.setMaxAge(60 * 60 * 24 * 3);
                    c2.setMaxAge(60 * 60 * 24 * 3);

                    response.addCookie(c1);
                    response.addCookie(c2);

                }

                return "/student/index";
            }
        }

        if ("admin".equals(type)) {
            Admin admin = adminService.login(username, password);
            if (admin == null) {
                model.addAttribute("msg", "管理员信息不存在");
                return "/login";
            } else {
                UserContext.putCurrebtUser(new Logininfo(admin.getUsername(), admin.getPassword()));
                model.addAttribute("admin", admin);

                if (params.get("remember") != null) {
                    // 账号信息保存三天
                    Cookie c1 = new Cookie("username", username);
                    Cookie c2 = new Cookie("password", password);
                    c1.setMaxAge(60 * 60 * 24 * 3);
                    c2.setMaxAge(60 * 60 * 24 * 3);

                    response.addCookie(c1);
                    response.addCookie(c2);

                }

                return "/admin/index";
            }
        }

        return "login";
    }


    @RequestMapping("/logout")
    public String logout() {

        HttpSession session = request.getSession();
        session.invalidate();
        return "login";

    }
}
